// SELECT mc.openid,mc.addtime, m.* FROM production.matrixbox m join production.matrixbox_collection mc on m.id = mc.itemid where openid = "sdgsdg" order by addtime DESC;
// SELECT mc.openid,mc.addtime, m.* FROM production.matrixbox m join production.matrixbox_collection mc on m.id = mc.itemid where openid = "sdgsdg" and keyword like "%报名%" order by addtime DESC ;

// 调用方法：
// $.post('http://http://api.juzhen05.com/admin/matrixbox/mycollection'


var path = require('path')
var routerApi = require('../../../utils/routeString.js')(__filename, __dirname);
var MatrixboxC = require('../dal/matrixbox_collection.js')

const addItem = async function (req, res) {
    var body = req.body || req.query;
    if (!body.openid) {
        return res.send({ code: 0, msg: '无效请求, openid参数出错' });
    }
    if (!body.itemid) {
        return res.send({ code: 0, msg: '无效请求, itemid参数出错' });
    }

    try {
        var mbc = new MatrixboxC({ openid: body.openid, itemid: body.itemid });
        var results = await mbc.add();
        return res.send({ code: 1, msg: "收藏成功" });
    } catch (error) {
        if(error.code === "ER_DUP_ENTRY"){
            return res.send({ code: 0, msg: "您已添收藏，无需重复添加" });
        }else{
            return res.send({ code: 0, msg: "收藏失败" });
        }
        
    }

}

const deleteItem = async function (req, res) {
    var body = req.body || req.query;
    if (!body.openid) {
        return res.send({ code: 0, msg: '无效请求, openid参数出错' });
    }
    if (!body.itemid) {
        return res.send({ code: 0, msg: '无效请求, itemid参数出错' });
    }

    var mbc = new MatrixboxC({ openid: body.openid, itemid: body.itemid });
    var results = await mbc.delete();
    return res.send({ code: 1, msg: "条目删除成功" });
}

const getList = async function (req, res) {
    var body = req.body || req.query;
    if (!body.openid) {
        return res.send({ code: 0, msg: '无效请求, openid参数出错' });
    }

    var mbc = new MatrixboxC({ openid: body.openid, itemid: body.itemid });
    var sqlHelper = mbc.sqlHelper;

    if (body.keyword) {
        var results = await sqlHelper.asyncQuery('SELECT mc.openid,mc.addtime, m.* FROM production.matrixbox m join production.matrixbox_collection mc on m.id = mc.itemid where openid = "' + body.openid + '" and keyword like "%' + body.keyword + '%"' + ' order by addtime DESC ;')
        return res.send({ code: 1, results: results });
    } else {
        var results = await sqlHelper.asyncQuery('SELECT mc.openid,mc.addtime, m.* FROM production.matrixbox m join production.matrixbox_collection mc on m.id = mc.itemid where openid = "' + body.openid + '" order by addtime DESC ;')
        return res.send({ code: 1, results: results });
    }

}

var analysisFn = async function (req, res, next) {
    var body = req.body || req.query;

    // 命令解析
    if (!body.cmd) {
        return res.send({ code: 0, msg: '无效请求, cmd参数出错' });
    }

    switch (body.cmd) {
        case "add":
            addItem(req, res);
            break;
        case "delete":
            deleteItem(req, res);
            break;
        case "getList":
            getList(req, res);
            break;
        default:
            return res.send({ code: 0, msg: '无效请求, cmd参数出错' });
    }
}


server.post({ path: routerApi, name: routerApi }, analysisFn);

